﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using ifunction.Jawbone.Up.SDK.Properties;

namespace ifunction.Jawbone.Up.SDK
{
    public partial class JawboneClient
    {
        /// <summary>
        /// Gets or sets the client unique identifier.
        /// </summary>
        /// <value>The client unique identifier.</value>
        public string ClientId
        {
            get;
            protected set;
        }

        /// <summary>
        /// Gets or sets the secret unique identifier.
        /// </summary>
        /// <value>The secret unique identifier.</value>
        public string SecretId
        {
            get;
            protected set;
        }

        /// <summary>
        /// Gets or sets the access token.
        /// </summary>
        /// <value>The access token.</value>
        protected string AccessToken
        {
            get;
            set;
        }

        /// <summary>
        /// Gets a value indicating whether this instance has token.
        /// </summary>
        /// <value><c>true</c> if this instance has token; otherwise, <c>false</c>.</value>
        public bool HasToken
        {
            get
            {
                return !string.IsNullOrWhiteSpace(AccessToken);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="JawboneClient"/> class.
        /// </summary>
        /// <param name="clientId">The client unique identifier.</param>
        /// <param name="secretId">The secret unique identifier.</param>
        public JawboneClient(string clientId, string secretId)
        {
            this.ClientId = clientId;
            this.SecretId = secretId;
        }

        public void Authenticate(string authorizationCode)
        {
            const string tokenUrl = "https://jawbone.com/auth/oauth2/token";

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(tokenUrl);
            Dictionary<string, string> post = new Dictionary<string, string>();

            post.Add("client_id", this.ClientId);
            post.Add("client_secret", this.SecretId);
            post.Add("grant_type", "authorization_code");
            request.FillData("POST", post);

        }

        protected string GenerateUrl(string relativeUrl)
        {
            return string.Format(
                   "https://Jawbone.com/nudge/api/{0}",
                     relativeUrl.GetStringValue());
        }

        protected HttpWebRequest GenerateWebRequest(string relativeUrl)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(GenerateUrl(relativeUrl));

            if (this.HasToken)
            {
                request.Headers.Add("Authorization", "Bearer " + this.AccessToken.GetStringValue());
            }

            return request;
        }
    }
}
